package br.unesp.rc.locadora.connection;

import com.sun.rowset.CachedRowSetImpl;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.rowset.CachedRowSet;

public class FabricaConexao implements Conexao {

    private FabricaConexao() {
    }

    public static Connection getConexao() {
        Connection con = null;

        try {
            Class.forName(DRIVER);
            con = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException ex) {
            System.out.println("Problemas ao carregar o Driver!");
            System.out.println("ERRO: " + ex.getMessage());
        } catch (SQLException ex) {
            System.out.println("Problemas ao autenticar!");
            System.out.println("ERRO: " + ex.getMessage());
        }

        return con;
    }

    public static CachedRowSet getConexaoCache() {
        CachedRowSet crs = null;
        
        try {
            crs = new CachedRowSetImpl();
            
            crs.setUrl(URL);  
            crs.setUsername(USER);  
            crs.setPassword(PASSWORD);  
            crs.setPageSize(1000);
        } catch (SQLException ex) {
            System.out.println("Problemas ao autenticar!");
            System.out.println("ERRO: " + ex.getMessage());
        }
        
        return crs;
    }

    public static Savepoint createSavepoint(Connection con){
        Savepoint save = null;
        
        try {
            save = con.setSavepoint();
        } catch (SQLException ex) {
            System.out.println("Problemas ao fazer rollback: " + ex.getMessage());
        }
        
        return save;
    }
    
    public static Savepoint createSavepoint(Connection con, String nome){
        Savepoint save = null;
        
        try {
            save = con.setSavepoint(nome);
        } catch (SQLException ex) {
            System.out.println("Problemas ao fazer rollback: " + ex.getMessage());
        }
        
        return save;
    }
    
    public static void rollback(Connection con) {
        try {
            con.rollback();
        } catch (SQLException ex) {
            System.out.println("Problemas ao fazer rollback: " + ex.getMessage());
        }
    }
    
    public static void rollback(Connection con, Savepoint save) {
        try {
            con.rollback(save);
        } catch (SQLException ex) {
            System.out.println("Problemas ao fazer rollback: " + ex.getMessage());
        }
    }
    
    public static void fechar(Connection con) {
        try {
            con.close();
        } catch (SQLException ex) {
            System.out.println("Problemas ao fechar: " + ex.getMessage());
        }
    }

    public static void fechar(Connection con, PreparedStatement pstm) {
        try {
            con.close();
            pstm.close();
        } catch (SQLException ex) {
            System.out.println("Problemas ao fechar: " + ex.getMessage());
        }
    }

    public static void fechar(PreparedStatement pstm) {
        try {
            pstm.close();
        } catch (SQLException ex) {
            System.out.println("Problemas ao fechar: " + ex.getMessage());
        }
    }
}
